﻿using Core.Container;
using Core.Interface;
using Core.MessageEvent;
using ModBus.Mode;
using ModBus.Views;
using Prism.Events;
using Prism.Ioc;
using Prism.Modularity;
using System.Collections.Generic;
using System.Net;
using System.Windows;

namespace ModBus
{
    public class ModBusModule : IModule
    {
        Container<ModBusDevice> Container;
        DataContainer<IPEndPoint> IPEndPoints;
        DataContainer<RTUParam> RTUParams;
        IEventAggregator _aggregator;

        public ModBusModule(IEventAggregator aggregator)
        {          
            _aggregator = aggregator;
            _aggregator.GetEvent<ClosingEvent>().Subscribe(Closing, ThreadOption.UIThread, true/*, filter => filter.Contains("Closing")*/);
        }

        public void OnInitialized(IContainerProvider containerProvider)
        {
            try
            {
                //var regionManager = containerProvider.Resolve<IRegionManager>();
                ////注册界面
                //regionManager.RegisterViewWithRegion("TCPDevices", typeof(DevicesView));
                //regionManager.RegisterViewWithRegion("RTUDevices", typeof(RTUDevicesView));
                //regionManager.RegisterViewWithRegion("ModbusTCP", typeof(ModBusTCPView));
                //regionManager.RegisterViewWithRegion("ModbusRTU", typeof(ModBusRTUView));
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }         
        }

        public void RegisterTypes(IContainerRegistry containerRegistry)
        {
            try
            {
                
                containerRegistry.RegisterDialog<SubView>("SubView");

                #region 因为界面有这行代码prism:ViewModelLocator.AutoWireViewModel="True"所以下面可注释掉
                //ViewModelLocationProvider.Register<ModBusTCPView, ModBusTCPViewModel>();
                //ViewModelLocationProvider.Register<ModBusRTUView, ModBusRTUViewModel>();
                //ViewModelLocationProvider.Register<DevicesView, DevicesViewModel>();
                //ViewModelLocationProvider.Register<RTUDevicesView, RTUDevicesViewModel>();
                //ViewModelLocationProvider.Register<SubView, SubViewModel>();
                #endregion

                IPEndPoints = new DataContainer<IPEndPoint>();
                IPEndPoints.Name = "IPEndPoints";
                IPEndPoints.Init();
                containerRegistry.RegisterInstance(IPEndPoints, "IPEndPoints");

                RTUParams = new DataContainer<RTUParam>();
                RTUParams.Name = "RTUParams";
                RTUParams.Init();
                containerRegistry.RegisterInstance(RTUParams, "RTUParams");

                //注册ModBusTCP设备到容器
                Container = new Container<ModBusDevice>();
                var keys = IPEndPoints.GetKeys();
                foreach (var key in keys)
                {
                    ModBusTCP modBusTCP = new ModBusTCP(key)
                    {
                        IPEndPoint = IPEndPoints.Get(key)
                    };
                    Container.Add(key, modBusTCP);
                }
                //注册ModBusRTU设备到容器
                var keys1 = RTUParams.GetKeys();
                foreach (var key in keys1)
                {
                    ModBusRTU modBusRTU = new ModBusRTU(key)
                    {
                        Param = RTUParams.Get(key)
                    };
                    Container.Add(key, modBusRTU);
                }

                containerRegistry.RegisterInstance(Container, "ModBusDevice");

                //注册导航
                containerRegistry.RegisterForNavigation<ModBusAllView>();
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }          
        }

        private void Closing()
        {
            if (Container!=null)
            {
                List<ModBusDevice> modBusTCPs = Container.GetValues();
                foreach (var item in modBusTCPs)
                {
                    item?.Close();
                }
            }      
        }

    }
}
